import { useMutation, useQuery } from "@apollo/client";
import { getUserInfo_service, updateUser_service } from "../../graphql/user";
import { userInfoType } from "../../types/user";

export function useGetUserInfo() {
  const { data, loading, refetch } = useQuery(getUserInfo_service, {
    fetchPolicy: "network-only",
  });
  return { data: data?.getStudentInfo.data as userInfoType, loading, refetch };
}

export function useEditUserInfo() {
  const [fun, { loading }] = useMutation(updateUser_service);

  const editUserInfo = async (params: {
    name?: string;
    desc?: string;
    tel?: string;
  }) => {
    const { data } = await fun({
      variables: { params },
    });
    return data.updateStudent as { code: boolean; message: string };
  };
  return { editLoading: loading, editUserInfo };
}
